# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ruby-ffi
_gemname=ffi
pkgver=1.15.5
pkgrel=1
pkgdesc="Ruby FFI library"
url="https://github.com/ffi/ffi"
arch="all"
license="BSD-3-Clause"
checkdepends="ruby-rspec"
makedepends="libffi-dev ruby ruby-dev"
install="$pkgname.post-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/ffi/$_gemname/archive/v$pkgver.tar.gz
	musl-compat.patch
	gemspec.patch
	"
builddir="$srcdir/$_gemname-$pkgver"

build() {
	gem build $_gemname.gemspec
	gem install --local \
		--install-dir dist \
		--ignore-dependencies \
		--no-document \
		--verbose \
		$_gemname-$pkgver.gem
}

check() {
	local exclude=''
	# FIXME fails with: expected -0.477986771... to be within 0.01 of 0.3
	case "$CARCH" in
		x86)  exclude='spec/ffi/long_double_spec.rb' ;;
	esac

	# LD_LIBRARY_PATH is a workaround to fix test
	# `Library ffi_lib interprets INPUT() in loader scripts` in library_spec.
	GEM_PATH='dist:.gem' LD_LIBRARY_PATH="$builddir/spec/ffi/fixtures" \
		rspec --exclude-pattern "$exclude"
}

package() {
	local gemdir="$pkgdir/$(ruby -e 'puts Gem.default_dir')"

	local carch
	case "$CARCH" in
		x86) carch="i386";;
		arm*) carch="arm";;
		ppc64le) carch="powerpc64";;
		*) carch="$CARCH";;
	esac

	cd "$builddir"/dist

	mkdir -p "$gemdir"
	cp -r extensions gems specifications "$gemdir"/

	# Remove unnecessary files and rubbish...
	find "$gemdir"/extensions/ -name mkmf.log -delete

	cd "$gemdir"/gems/$_gemname-$pkgver
	rm -rf ext/ lib/*.so
	find lib/ffi/platform/* -prune ! -name "$carch-linux" -exec rm -rf {} +
}

sha512sums="
3668bafae936bf5010f014fbcf63258b023bcc318882775e7d2fcfc11334722ec2b6e8a8b9c910d406889fb173dd503c5c13f5af8e4f6927921f12f688461707  ruby-ffi-1.15.5.tar.gz
c4400693bd34ade9a06af86e564dee35ee3e3fcd59adec135531c8ebaca7c70e8f4082272f7ae6356bab8c9c5e545cc889d553ac6bf5fb823568cc743a647d62  musl-compat.patch
7e14c924c720eb34ec1f95fe385ff1e535193522133af93c1591b3d049f05027ba31ca284ffb7a2c68935d0fe6472d666e2f313931f662d669d2116ef03ebb3b  gemspec.patch
"
